package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.Unit;
import com.service.UnitService;

/**
 * @author author : 张鹏
 * @date createTime：2018年1月10日 下午10:46:47
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("unit")
public class UnitController {
	@Autowired
	private UnitService unitService;

	Unit unit;

	List<Unit> unitList;

	/**
	 * 查询所有计量单位
	 * 
	 * @return
	 */
	@RequestMapping("getAllUnit")
	public String getAllUnit(HttpServletRequest request) {
		List<Unit> list = unitService.getAllUnit();
		request.setAttribute("unitList", list);
		return "unitManage";
	}

	/**
	 * 查询所有计量单位 ajax
	 * 
	 * @return
	 */
	@RequestMapping(value="getAllUnitAjax",produces="application/json;charset=UTF-8")
	@ResponseBody
	public List<Unit> getAllUnitAjax() {
		return unitService.getAllUnit();
	}

	/**
	 * 创建新的计量单位
	 * 
	 * @param unitName
	 * @return
	 */
	@RequestMapping("createNewUnitAjax")
	@ResponseBody
	public String createNewUnit(String unitName) {
		unit = new Unit();
		unit.setUnitName(unitName);
		return unitService.createNewUnit(unit) + "";
	}

	/**
	 * 编辑计量单位
	 * 
	 * @param unitId
	 * @param unitName
	 * @return
	 */
	@RequestMapping("editUnitAjax")
	@ResponseBody
	public String editUnit(int unitId, String unitName) {
		unit = new Unit();
		unit.setUnitId(unitId);
		unit.setUnitName(unitName);
		return unitService.editUnit(unit) + "";
	}

	/**
	 * 删除计量单位
	 * 
	 * @param unitId
	 * @return
	 */
	@RequestMapping("deleteUnitAjax")
	@ResponseBody
	public String deleteUnit(int unitId) {
		return unitService.deleteUnit(unitId) + "";
	}

}
